$(function() {

	$('#li_sv').addClass('active open');
	$('#svmonitor').addClass('active');

	initCpuChart();

	// initHBChart();

	// row click event
	clicktable = function(row) {
		$
				.ajax({
					type : 'post',
					url : 'serverselect',
					dataType : 'text',
					data : {
						"nameAndIp" : row.id
					},
					success : function(msg) {
						var data = JSON.parse(msg);
						var map = JSON.parse(data.quota);
						var disk = JSON.parse(data.disk);

						// console.log(map)
						// console.log(disk);

						// change disk chart
						var diskrow = $('#diskrow');
						// console.log(diskrow);
						// var child = diskrow.children();
						// console.log(child);

						var test = document.getElementById("diskrow");
						while (test.hasChildNodes()) {
							test.removeChild(test.firstChild);
						}

						var rowbody = '';
						var diskname;
						var disused;
						for ( var i in disk) {
							diskname = disk[i].diskname;
							disused = disk[i].diskused;

							// console.log(diskname + ' ' + disused);

							rowbody += '<div class="span3"><div class="easy-pie-chart"><div class="number disk" data-percent='
									+ disused
									+ '><span>'
									+ disused
									+ '</span></div><a class="title" href="#">'
									+ diskname + '</a></div></div>';
						}
						test.innerHTML = rowbody;

						$('.easy-pie-chart .number.disk').easyPieChart({
							animate : 1000,
							size : 75,
							lineWidth : 3,
							barColor : '#ffb848'
						});

						// change mem chart
						$('#mem_pie').attr('data-percent',
								map.memused * 100 / map.memtotal);
						var mem_chart = window.chart = $('#mem_pie').data(
								'easyPieChart');
						mem_chart.update(map.memused * 100 / map.memtotal);

						$('#cmemtip').html(
								parseInt(map.memused * 100 / map.memtotal)
										+ '%');
						$("#memtip").html(
								'使用内存/内存总量:&nbsp;&nbsp;' + map.memused + '/'
										+ map.memtotal + 'MB');

						// change cpu chart
						var freshCpuData = [ map.cpu1, map.cpu2, map.cpu3,
								map.cpu4, map.cpu5 ];
						var cpuChart = echarts.init(document
								.getElementById("cpucharts"));
						cpuOption = {
							xAxis : {
								type : 'category',
								boundaryGap : false,
								data : [ '1', '2', '3', '4', '5' ]
							},
							yAxis : {
								type : 'value',
								axisLabel : {
									formatter : '{value} %'
								}
							},
							series : [ {
								name : '最高负载',
								type : 'line',
								data : freshCpuData,
								markPoint : {
									data : [ {
										type : 'max',
										name : '最大值'
									}, {
										type : 'min',
										name : '最小值'
									} ]
								},
								markLine : {
									data : [ {
										type : 'average',
										name : '平均值'
									} ]
								}
							} ]
						};
						cpuChart.setOption(cpuOption);

						// heart beat chart
						var hbxaix = JSON.parse(data.heartbeat);
						if (data.hcount == 0) {
							document.getElementById("hbdiv").style.display = "none";
						} else {
							var val = [];
							var nlist;
							var nnlist;
							var bnlist;

							var nowtime = new Date();
							val.push([ nowtime.format('MM-dd hh:mm:ss'), 0 ]);

							for (var i = 0; i < data.hcount; i++) {
								var timestamp = Date.parse(new Date(hbxaix[i]));
								var newDate = new Date();
								newDate.setTime(timestamp);

								nlist = [ newDate.format('MM-dd hh:mm:ss'), 1 ];

								nt = addMinutes(newDate, 5);
								bnt = addMinutes(newDate, -5);

								nnlist = [ nt.format('MM-dd hh:mm:ss'), 0 ];
								bnlist = [ bnt.format('MM-dd hh:mm:ss'), 0 ];

								val.push(nnlist);
								val.push(nlist);
								val.push(bnlist);
							}

							// console.log(val);

							// delete first element if 2rd > 1st
							if (val[0][0] <= val[1][0]) {
								val.splice(0, 1);
							}

							var hbChart = echarts.init(document
									.getElementById("hbcharts"));
							hbOption = {
								xAxis : {
									type : 'time',
									axisLabel : {
										interval : 0,
										rotate : 40
									}
								},
								yAxis : {
									show : false,
									type : 'value',
									max : 1.3,
									min : 0,
									axisLabel : {
										formatter : '{value}'
									}
								},
								series : [ {
									name : '心跳监测',
									type : 'line',
									smooth : true,
									// symbol : 'none',
									data : val,
									animation : false
								} ]
							};
							hbChart.setOption(hbOption);

							document.getElementById("hbdiv").style.display = "";
						}
					},
					error : function(msg) {
						console.log('failed');
					}
				});

	}

	function initCpuChart() {
		var mapval = $("#quotaValue").val();
		var quo = JSON.parse(mapval);
		var cpudata = [ quo.cpu1, quo.cpu2, quo.cpu3, quo.cpu4, quo.cpu5 ];

		var cpuChart = echarts.init(document.getElementById("cpucharts"));
		cpuOption = {
			title : {
				text : '负载浮动',
			},
			tooltip : {
				trigger : 'axis'
			},
			legend : {
				data : [ '最高负载' ]
			},
			toolbox : {
				show : true,
				feature : {
					dataZoom : {
						yAxisIndex : 'none'
					},
					dataView : {
						readOnly : false
					},
					magicType : {
						type : [ 'line', 'bar' ]
					},
					restore : {},
					saveAsImage : {}
				}
			},
			xAxis : {
				type : 'category',
				boundaryGap : false,
				data : [ '1', '2', '3', '4', '5' ]
			},
			yAxis : {
				type : 'value',
				axisLabel : {
					formatter : '{value} %'
				}
			},
			series : [ {
				name : '最高负载',
				type : 'line',
				data : cpudata,
				markPoint : {
					data : [ {
						type : 'max',
						name : '最大值'
					}, {
						type : 'min',
						name : '最小值'
					} ]
				},
				markLine : {
					data : [ {
						type : 'average',
						name : '平均值'
					} ]
				}
			} ]
		};
		cpuChart.setOption(cpuOption);
	}

	function initHBChart() {
		var hbChart = echarts.init(document.getElementById("hbcharts"));
		hbOption = {
			xAxis : {
				type : 'time',
				boundaryGap : false,
				data : []
			},
			yAxis : {
				type : 'value',
				axisLabel : {
					formatter : '{value}'
				}
			},
			series : [ {
				name : '心跳监测',
				type : 'line',
				data : []
			} ]
		};
		hbChart.setOption(hbOption);
	}

});